/**
 * 分类数据管理 Composable
 */

import type { Category } from '~/types'

/**
 * 使用分类数据
 */
export const useCategories = () => {
  const { $api } = useNuxtApp()

  /**
   * 获取所有分类
   */
  const getCategories = async (): Promise<Category[]> => {
    return await $api('/categories')
  }

  /**
   * 获取单个分类
   */
  const getCategory = async (id: number): Promise<Category> => {
    return await $api(`/categories/${id}`)
  }

  /**
   * 通过 slug 获取分类
   */
  const getCategoryBySlug = async (slug: string): Promise<Category> => {
    return await $api(`/categories/slug/${slug}`)
  }

  return {
    getCategories,
    getCategory,
    getCategoryBySlug,
  }
}


